﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagerSystem
{
    class ScoreCompare : IComparer<Student>
    {
        private E_CourseType CompareCourse;
        public ScoreCompare(E_CourseType course)
        {
            CompareCourse = course;
        }
        public int Compare(Student x, Student y)
        {
            if (x.Score.GetScore(CompareCourse) > y.Score.GetScore(CompareCourse))
                return -1;
            else if (x.Score.GetScore(CompareCourse) < y.Score.GetScore(CompareCourse))
                return 1;
            else
                return 0;
        }
    }
    /// <summary>
    /// 成绩
    /// </summary>
    public class Score
    {
        private Dictionary<E_CourseType, float> dicScore;

        public Score()
        {
            dicScore = new Dictionary<E_CourseType, float>();
        }
        /*
        #region 第一种做法

        /// <summary>
        /// 英语
        /// </summary>
        private float _English;
        public float English
        {
            get { return _English; }
            set { _English = VerifyValidRange(value); }
        }
        /// <summary>
        /// 中文
        /// </summary>
        private float _Chinese;
        public float Chinese
        {
            get { return _Chinese; }
            set { _Chinese = VerifyValidRange(value); }
        }
        /// <summary>
        /// 地理
        /// </summary>
        private float _Geography;
        public float Geography
        {
            get { return _Geography; }
            set { _Geography = VerifyValidRange(value); }
        }
        /// <summary>
        /// 历史
        /// </summary>
        private float _History;
        public float History
        {
            get { return _History; }
            set { _History = VerifyValidRange(value); }
        }
        /// <summary>
        /// 生物
        /// </summary>
        private float _Biology;
        public float Biology
        {
            get { return _Biology; }
            set { _Biology = VerifyValidRange(value); }
        }

        /// <summary>
        /// 化学
        /// </summary>
        private float _Chemistry;
        public float Chemistry
        {
            get { return _Chemistry; }
            set { _Chemistry = VerifyValidRange(value); }
        }

        #endregion
        */

        #region 第二种做法

        /// <summary>
        /// 英语
        /// </summary>
        public float English
        {
            get { return GetScore(E_CourseType.English); }
            set { SetScore(E_CourseType.English, value); }
        }
        /// <summary>
        /// 中文
        /// </summary>
        public float Chinese
        {
            get { return GetScore(E_CourseType.Chinese); }
            set { SetScore(E_CourseType.Chinese, value); }
        }
        /// <summary>
        /// 地理
        /// </summary>
        public float Geography
        {
            get { return GetScore(E_CourseType.Geography); }
            set { SetScore(E_CourseType.Geography, value); }
        }
        /// <summary>
        /// 历史
        /// </summary>
        public float History
        {
            get { return GetScore(E_CourseType.History); }
            set { SetScore(E_CourseType.History, value); }
        }
        /// <summary>
        /// 生物
        /// </summary>
        public float Biology
        {
            get { return GetScore(E_CourseType.Biology); }
            set { SetScore(E_CourseType.Biology, value); }
        }

        /// <summary>
        /// 化学
        /// </summary>
        public float Chemistry
        {
            get { return GetScore(E_CourseType.Chemistry); }
            set { SetScore(E_CourseType.Chemistry, value); }
        }

        //总分
        public float All
        {
            get
            {
                float ret = 0.0f;
                for (E_CourseType i = E_CourseType.None+1; i < E_CourseType.All; i++)
                {
                    ret = ret + GetScore(i);
                }
                return ret;
            }
            set { }
        }

        //平均分
        public float Average
        {
            get
            {
                return All / (int)(E_CourseType.All - 1);
            }
        }

        #endregion

        /// <summary>
        /// 根据类型设置成绩
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        public void SetScore(E_CourseType type, float value)
        {
            if (type == E_CourseType.All)
            {
                All = value;
                return;
            }
            value = VerifyValidRange(value);
            dicScore[type] = value;
        }

        /// <summary>
        /// 根据类型获取成绩
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public float GetScore(E_CourseType type)
        {
            if (type == E_CourseType.All)
                return All;
            float ret;
            if (dicScore.TryGetValue(type, out ret))
                return ret;
            return 0.0f;
        }

        /// <summary>
        /// 验证分数有效性
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private float VerifyValidRange(float value)
        {
            if (value < 0) 
               return 0;
            if (value > 150)
               return 150;
            return value;
        }
    }
}
